{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "906062ff",
   "metadata": {},
   "source": [
    "# 读取DataPattern下的文件\n",
    "\n",
    "注意：所有JPG图像都会被认为是训练集。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "e10397f8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>filename</th>\n",
       "      <th>label_id</th>\n",
       "      <th>label</th>\n",
       "      <th>info</th>\n",
       "      <th>ID</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0\\0_6656_10240_0_0_512_512.jpg</td>\n",
       "      <td>0</td>\n",
       "      <td>UNKNOWN</td>\n",
       "      <td>{}</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0\\0_6656_10240_0_512_512_1024.jpg</td>\n",
       "      <td>0</td>\n",
       "      <td>UNKNOWN</td>\n",
       "      <td>{}</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0\\0_6656_10240_0_1024_512_1536.jpg</td>\n",
       "      <td>0</td>\n",
       "      <td>UNKNOWN</td>\n",
       "      <td>{}</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0\\0_6656_10240_0_1536_512_2048.jpg</td>\n",
       "      <td>0</td>\n",
       "      <td>UNKNOWN</td>\n",
       "      <td>{}</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0\\0_6656_10240_0_2048_512_2560.jpg</td>\n",
       "      <td>0</td>\n",
       "      <td>UNKNOWN</td>\n",
       "      <td>{}</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1759</th>\n",
       "      <td>99\\99_25088_18432_24576_15872_25088_16384.jpg</td>\n",
       "      <td>0</td>\n",
       "      <td>UNKNOWN</td>\n",
       "      <td>{}</td>\n",
       "      <td>99</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1760</th>\n",
       "      <td>99\\99_25088_18432_24576_16384_25088_16896.jpg</td>\n",
       "      <td>0</td>\n",
       "      <td>UNKNOWN</td>\n",
       "      <td>{}</td>\n",
       "      <td>99</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1761</th>\n",
       "      <td>99\\99_25088_18432_24576_16896_25088_17408.jpg</td>\n",
       "      <td>0</td>\n",
       "      <td>UNKNOWN</td>\n",
       "      <td>{}</td>\n",
       "      <td>99</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1762</th>\n",
       "      <td>99\\99_25088_18432_24576_17408_25088_17920.jpg</td>\n",
       "      <td>0</td>\n",
       "      <td>UNKNOWN</td>\n",
       "      <td>{}</td>\n",
       "      <td>99</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1763</th>\n",
       "      <td>99\\99_25088_18432_24576_17920_25088_18432.jpg</td>\n",
       "      <td>0</td>\n",
       "      <td>UNKNOWN</td>\n",
       "      <td>{}</td>\n",
       "      <td>99</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>708996 rows × 5 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                           filename  label_id    label info  \\\n",
       "0                    0\\0_6656_10240_0_0_512_512.jpg         0  UNKNOWN   {}   \n",
       "1                 0\\0_6656_10240_0_512_512_1024.jpg         0  UNKNOWN   {}   \n",
       "2                0\\0_6656_10240_0_1024_512_1536.jpg         0  UNKNOWN   {}   \n",
       "3                0\\0_6656_10240_0_1536_512_2048.jpg         0  UNKNOWN   {}   \n",
       "4                0\\0_6656_10240_0_2048_512_2560.jpg         0  UNKNOWN   {}   \n",
       "...                                             ...       ...      ...  ...   \n",
       "1759  99\\99_25088_18432_24576_15872_25088_16384.jpg         0  UNKNOWN   {}   \n",
       "1760  99\\99_25088_18432_24576_16384_25088_16896.jpg         0  UNKNOWN   {}   \n",
       "1761  99\\99_25088_18432_24576_16896_25088_17408.jpg         0  UNKNOWN   {}   \n",
       "1762  99\\99_25088_18432_24576_17408_25088_17920.jpg         0  UNKNOWN   {}   \n",
       "1763  99\\99_25088_18432_24576_17920_25088_18432.jpg         0  UNKNOWN   {}   \n",
       "\n",
       "      ID  \n",
       "0      0  \n",
       "1      0  \n",
       "2      0  \n",
       "3      0  \n",
       "4      0  \n",
       "...   ..  \n",
       "1759  99  \n",
       "1760  99  \n",
       "1761  99  \n",
       "1762  99  \n",
       "1763  99  \n",
       "\n",
       "[708996 rows x 5 columns]"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import os\n",
    "import pandas as pd\n",
    "\n",
    "from onekey_algo import OnekeyDS as okds\n",
    "from onekey_algo import get_param_in_cwd\n",
    "\n",
    "data_pattern = get_param_in_cwd('data_pattern')\n",
    "patch_info = []\n",
    "for r, ds, fs in os.walk(data_pattern):\n",
    "    patch_info.extend([pd.read_csv(os.path.join(r, p)) for p in fs if p.endswith('.csv')])\n",
    "\n",
    "patches = pd.concat(patch_info, axis=0)\n",
    "patches['ID'] = patches['filename'].map(lambda x: os.path.dirname(x))\n",
    "\n",
    "# def map2id(x):\n",
    "#     x = os.path.basename(os.path.dirname(x))\n",
    "# #     *fname_items, w, h, ltx, lty, rbx, rby = x.split('_')\n",
    "# #     return '_'.join(fname_items)\n",
    "#     return x\n",
    "# patches['ID'] = patches['fpath'].map(map2id)\n",
    "patches"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "20be592e",
   "metadata": {},
   "source": [
    "# 获取划分的数据集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "066a6b79",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>filename</th>\n",
       "      <th>label_id</th>\n",
       "      <th>label_x</th>\n",
       "      <th>info</th>\n",
       "      <th>ID</th>\n",
       "      <th>label_y</th>\n",
       "      <th>group</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0\\0_6656_10240_0_0_512_512.jpg</td>\n",
       "      <td>0</td>\n",
       "      <td>UNKNOWN</td>\n",
       "      <td>{}</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>train</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0\\0_6656_10240_0_512_512_1024.jpg</td>\n",
       "      <td>0</td>\n",
       "      <td>UNKNOWN</td>\n",
       "      <td>{}</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>train</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0\\0_6656_10240_0_1024_512_1536.jpg</td>\n",
       "      <td>0</td>\n",
       "      <td>UNKNOWN</td>\n",
       "      <td>{}</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>train</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0\\0_6656_10240_0_1536_512_2048.jpg</td>\n",
       "      <td>0</td>\n",
       "      <td>UNKNOWN</td>\n",
       "      <td>{}</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>train</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0\\0_6656_10240_0_2048_512_2560.jpg</td>\n",
       "      <td>0</td>\n",
       "      <td>UNKNOWN</td>\n",
       "      <td>{}</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>train</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>708991</th>\n",
       "      <td>99\\99_25088_18432_24576_15872_25088_16384.jpg</td>\n",
       "      <td>0</td>\n",
       "      <td>UNKNOWN</td>\n",
       "      <td>{}</td>\n",
       "      <td>99</td>\n",
       "      <td>1</td>\n",
       "      <td>train</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>708992</th>\n",
       "      <td>99\\99_25088_18432_24576_16384_25088_16896.jpg</td>\n",
       "      <td>0</td>\n",
       "      <td>UNKNOWN</td>\n",
       "      <td>{}</td>\n",
       "      <td>99</td>\n",
       "      <td>1</td>\n",
       "      <td>train</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>708993</th>\n",
       "      <td>99\\99_25088_18432_24576_16896_25088_17408.jpg</td>\n",
       "      <td>0</td>\n",
       "      <td>UNKNOWN</td>\n",
       "      <td>{}</td>\n",
       "      <td>99</td>\n",
       "      <td>1</td>\n",
       "      <td>train</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>708994</th>\n",
       "      <td>99\\99_25088_18432_24576_17408_25088_17920.jpg</td>\n",
       "      <td>0</td>\n",
       "      <td>UNKNOWN</td>\n",
       "      <td>{}</td>\n",
       "      <td>99</td>\n",
       "      <td>1</td>\n",
       "      <td>train</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>708995</th>\n",
       "      <td>99\\99_25088_18432_24576_17920_25088_18432.jpg</td>\n",
       "      <td>0</td>\n",
       "      <td>UNKNOWN</td>\n",
       "      <td>{}</td>\n",
       "      <td>99</td>\n",
       "      <td>1</td>\n",
       "      <td>train</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>708996 rows × 7 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                             filename  label_id  label_x info  \\\n",
       "0                      0\\0_6656_10240_0_0_512_512.jpg         0  UNKNOWN   {}   \n",
       "1                   0\\0_6656_10240_0_512_512_1024.jpg         0  UNKNOWN   {}   \n",
       "2                  0\\0_6656_10240_0_1024_512_1536.jpg         0  UNKNOWN   {}   \n",
       "3                  0\\0_6656_10240_0_1536_512_2048.jpg         0  UNKNOWN   {}   \n",
       "4                  0\\0_6656_10240_0_2048_512_2560.jpg         0  UNKNOWN   {}   \n",
       "...                                               ...       ...      ...  ...   \n",
       "708991  99\\99_25088_18432_24576_15872_25088_16384.jpg         0  UNKNOWN   {}   \n",
       "708992  99\\99_25088_18432_24576_16384_25088_16896.jpg         0  UNKNOWN   {}   \n",
       "708993  99\\99_25088_18432_24576_16896_25088_17408.jpg         0  UNKNOWN   {}   \n",
       "708994  99\\99_25088_18432_24576_17408_25088_17920.jpg         0  UNKNOWN   {}   \n",
       "708995  99\\99_25088_18432_24576_17920_25088_18432.jpg         0  UNKNOWN   {}   \n",
       "\n",
       "        ID  label_y  group  \n",
       "0        0        0  train  \n",
       "1        0        0  train  \n",
       "2        0        0  train  \n",
       "3        0        0  train  \n",
       "4        0        0  train  \n",
       "...     ..      ...    ...  \n",
       "708991  99        1  train  \n",
       "708992  99        1  train  \n",
       "708993  99        1  train  \n",
       "708994  99        1  train  \n",
       "708995  99        1  train  \n",
       "\n",
       "[708996 rows x 7 columns]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "label_f = pd.read_csv(get_param_in_cwd('label_file'), dtype={'ID': str})\n",
    "# label_f['ID'] = label_f['ID'].map(lambda x: str(x).replace('.nii.gz', ''))\n",
    "patches = pd.merge(patches, label_f, on='ID', how='inner')\n",
    "patches"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b5b66801",
   "metadata": {},
   "source": [
    "# 保存弱监督List模式数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "b8f8a0c1",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "train_patches = patches[patches['group'] == 'train']\n",
    "train_patches[['filename', 'label_y']].to_csv(os.path.join('split_info', f'semi_train.txt'), index=False, header=False, sep='\\t')\n",
    "test_patches = patches[patches['group'] != 'train']\n",
    "test_patches[['filename', 'label_y']].to_csv(os.path.join('split_info', f'semi_val.txt'), index=False, header=False, sep='\\t')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8c628e6c",
   "metadata": {},
   "source": [
    "# 保存监督List模式数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "615c99c9",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "roi = get_param_in_cwd('supervise_label')\n",
    "if roi is None:\n",
    "    patches = patches[patches['label_x'] != '__background__']\n",
    "else:\n",
    "    patches = patches[patches['label_x'] == roi]\n",
    "    \n",
    "train_patches = patches[patches['group'] == 'train']\n",
    "train_patches[['filename', 'label_y']].to_csv(os.path.join('split_info', f'full_train.txt'), index=False, header=False, sep='\\t')\n",
    "test_patches = patches[patches['group'] != 'train']\n",
    "test_patches[['filename', 'label_y']].to_csv(os.path.join('split_info', f'full_val.txt'), index=False, header=False, sep='\\t')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "58790652",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
